package com.example.servicetest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ComputeService  extends Service {

   private MyBinder myBinder;
   private Handler handler;
    @SuppressLint("HandlerLeak")
    @Override
    public void onCreate() {
        super.onCreate();
        myBinder = new MyBinder();
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch (msg.what){
                    case 0:

                }
            }
        };
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return myBinder;
    }



    private void addCompute(final int a, final int b, final Context context){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 0;
                message.arg1 = (a + b);
               //handler.sendMessage(message);
                ((Activity)context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context,"Result "+(a + b),Toast.LENGTH_SHORT).show();
                    }
                });

            }
        };

        new Thread(runnable).start();
    }



    public class MyBinder extends Binder{

        public void add(final int a, final int b,Context context){

            addCompute(a,b,context);
        }

    }
}
